﻿Public Class Login
    Dim ja As Int16
    Dim tbl As New DataTable
    Dim tbl2 As New DataTable
    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
        Dim i As Integer = 0
        Dim msg As String = ""
        If ja > 5 Then
            MessageBox.Show("You enter wrong username or password more than 5 times this program quit now")
            Me.Close()
        End If
        ja = ja + 1
        If txtuser.Text = "" Or txtpwd.Text = "" Then
            MessageBox.Show("Please input username and password")
            Exit Sub
        ElseIf txtuser.Text <> "" And txtpwd.Text <> "" Then
            For i = 0 To tbl.Rows.Count - 1
                If LCase(tbl.Rows(i).Item(2)) = LCase(txtuser.Text) And tbl.Rows(i).Item(3) = txtpwd.Text Then
                    MessageBox.Show("Login Success")

                    MainMenu.empid = tbl.Rows(i).Item(0)
                    MainMenu.empname = tbl.Rows(i).Item(1)
                    MainMenu.usertypeid = tbl.Rows(i).Item(5)
                    MainMenu.usertype = tbl.Rows(i).Item(6)
                    MainMenu.username = tbl.Rows(i).Item(2)
                    MainMenu.userid = tbl.Rows(i).Item(4)
                    MainMenu.adjust = tbl.Rows(i).Item(7)
                    MainMenu.Admin = tbl.Rows(i).Item(8)
                    MainMenu.candidate = tbl.Rows(i).Item(9)
                    MainMenu.employee = tbl.Rows(i).Item(10)

                    cmd.Parameters("@id") = tbl.Rows(i).Item(4)
                    tbl2 = cmd.ExecuteDataTable("SELECT * FROM user_role_user where user_id=@id")
                    If tbl2.Rows.Count > 0 Then
                        If tbl2.Rows(0).Item(2) = True Then
                            MainMenu.employee = tbl2.Rows(0).Item(2)
                        End If
                        If tbl2.Rows(0).Item(3) = True Then
                            MainMenu.adjust = tbl2.Rows(0).Item(3)
                        End If
                        If tbl2.Rows(0).Item(4) = True Then
                            MainMenu.candidate = tbl2.Rows(0).Item(4)
                        End If
                        If tbl2.Rows(0).Item(5) = True Then
                            MainMenu.Admin = tbl2.Rows(0).Item(5)
                        End If
                    End If
                    MainMenu.Show()
                    Me.Close()
                    msg = ""
                    Exit For
                Else
                    msg = "Login fail"
                End If
            Next
            If msg <> "" Then
                MessageBox.Show(msg)
            End If

        Else
            MessageBox.Show("Login Fail")
        End If
    End Sub

    Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tbl = cmd.ExecuteDataTable("SELECT u.emp_id,emp.emp_name,u.username,u.password,u.user_id,ut.user_type_id,ut.user_type,ur.Adjust,ur.Admin,ur.Candidate,ur.Employee FROM user_tbl u INNER JOIN employee_tbl emp on u.emp_id=emp.emp_id INNER JOIN user_type_tbl ut on ut.user_type_id=u.user_type_id INNER JOIN user_role_group ur on ur.user_type_id=ut.user_type_id")
    End Sub
End Class